﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
//
using System.Configuration;
using EncryptProject;
using System.Diagnostics;


namespace DBLib
{
    /// <summary>
    /// 数据连接类，本系统采用 Access2003 数据库
    /// </summary>
    public class DataConn
    {
        #region 变量

        private static string decryptConnectionString; // 解密后的连接字符串

        #endregion

        #region 私有方法

        /// <summary>
        /// 获取 App.config 中的连接字符串【静态方法】
        /// </summary>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <returns></returns>
        private static string GetConnectionString(string connectionStringName,
            string mainProjectEXE)
        {
            if (string.IsNullOrWhiteSpace(connectionStringName) ||
                string.IsNullOrWhiteSpace(mainProjectEXE))
            {
#if DEBUG
                Debug.WriteLine("连接字符串数据库名称 | 主工程可执行文件名称 不正确！");
#endif
                return string.Empty;
            }

            string encryptConnectionString; // 获得加密连接字符串
            //encryptConnectionString = ConfigurationManager.ConnectionStrings["db_VWMSConnectionString"].ConnectionString; // 此句：仅对本工程有效，跨工程会找不到主工程的 app.config
            Configuration config = ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(Environment.CurrentDirectory, mainProjectEXE)); // 可针对不同工程发布到同一文件夹情况
            encryptConnectionString = config.ConnectionStrings.ConnectionStrings[connectionStringName].ConnectionString;

            try
            {
                decryptConnectionString = EncryptAlgorithmHelper.Decrypt2(encryptConnectionString); // 获得解密后的连接字符串
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                decryptConnectionString = string.Empty;
            }

            return decryptConnectionString;
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <returns></returns>
        public OleDbConnection GetOleDbConnection(string connectionStringName,
            string mainProjectEXE)
        {
            //string strDPath = Application.StartupPath;
            //string strDataSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDPath.Substring(0, strDPath.LastIndexOf("\\")).Substring(0, strDPath.Substring(0, strDPath.LastIndexOf("\\")).LastIndexOf("\\")) + "\\DataBase\\db_VWMS.mdb;Jet OLEDB:Database Password=Fanxuanxuan123";
            //eg: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DataBase\db_VWMS.mdb;Persist Security Info=True;Jet OLEDB:Database Password=Fanxuanxuan123
            if (string.IsNullOrWhiteSpace(connectionStringName))
                return null;
            
            OleDbConnection oledbCon;
            if(string.IsNullOrWhiteSpace(mainProjectEXE))
                oledbCon = new OleDbConnection(connectionStringName); // 直接使用连接字符串  
            else
                oledbCon = new OleDbConnection(GetConnectionString(connectionStringName, mainProjectEXE));
            
            return oledbCon;
        }

        #endregion
    }
}
